﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.HVAC;
using HYCreateDuct.FormDuct;
using YRevitJig;

namespace HYCreateDuct.MethodCreateDuct
{
	public class DuctVJigPolarAxis : PELineJig
	{
		public new DuctVJigPolarAxis.EStage JigStage { get; set; }

		[DllImport("user32.dll")]
		private static extern int SetCursorPos(int x, int y);

		[DllImport("User32.dll")]
		public static extern int GetCursorPos(out System.Drawing.Point point);

		public static System.Drawing.Point ScreenCursorPosition
		{
			get
			{
				System.Drawing.Point result = default(System.Drawing.Point);
				DuctVJigPolarAxis.GetCursorPos(out result);
				return result;
			}
		}

		public new XYZ StartPoint { get; set; }

		public DuctVJigPolarAxis(UIDocument uiDoc, FormCreateDuct mcdt) : base(uiDoc)
		{
			this.cdt = mcdt;
		}

		public new XYZ PickStartPoint(string strPrompt = "请确定布置点：")
		{
			try
			{
				this.blStauts = true;
				this.JigStage = DuctVJigPolarAxis.EStage.eCenterPt;
				this.StartPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				return null;
			}
			return this.StartPoint;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && paintEventArgs != null && this.blStauts)
			{
				try
				{
					XYZ ptCur = base.Client2Revit(this.m_ptCur);
					DuctWindowStruct data = this.cdt.GetData(true);
					this.dWidth = data.dDuctWidth / 304.8;
					this.dHeight = data.dDuctHeight / 304.8;
					this.StartPoint = XYZ.Zero;
					this.StartPoint = this.SetPtCur(ptCur, this.dWidth, this.dHeight);
					if (this.dHeight == 0.0)
					{
						this.nType = 2;
					}
					else
					{
						this.nType = 1;
					}
					if (this.nType == 1)
					{
						XYZ point = new XYZ(this.StartPoint.X - this.dWidth / 2.0, this.StartPoint.Y - this.dHeight / 2.0, 0.0);
						XYZ point2 = new XYZ(this.StartPoint.X + this.dWidth / 2.0, this.StartPoint.Y - this.dHeight / 2.0, 0.0);
						XYZ point3 = new XYZ(this.StartPoint.X + this.dWidth / 2.0, this.StartPoint.Y + this.dHeight / 2.0, 0.0);
						XYZ point4 = new XYZ(this.StartPoint.X - this.dWidth / 2.0, this.StartPoint.Y + this.dHeight / 2.0, 0.0);
						System.Drawing.Point point5 = base.Revit2Client(point);
						System.Drawing.Point point6 = base.Revit2Client(point2);
						System.Drawing.Point point7 = base.Revit2Client(point3);
						System.Drawing.Point point8 = base.Revit2Client(point4);
						XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						new XLine2D(point5, point6).Draw(g);
						new XLine2D(point6, point7).Draw(g);
						new XLine2D(point7, point8).Draw(g);
						new XLine2D(point8, point5).Draw(g);
					}
					else
					{
						XYZ point9 = new XYZ(this.StartPoint.X - this.dWidth / 2.0, this.StartPoint.Y, 0.0);
						XYZ point10 = new XYZ(this.StartPoint.X + this.dWidth / 2.0, this.StartPoint.Y, 0.0);
						XYZ point11 = new XYZ(this.StartPoint.X, this.StartPoint.Y + this.dWidth / 2.0, 0.0);
						XYZ point12 = new XYZ(this.StartPoint.X, this.StartPoint.Y - this.dWidth / 2.0, 0.0);
						System.Drawing.Point ptStart = base.Revit2Client(point9);
						System.Drawing.Point ptEnd = base.Revit2Client(point10);
						System.Drawing.Point ptOnArc = base.Revit2Client(point11);
						System.Drawing.Point ptOnArc2 = base.Revit2Client(point12);
						XGraphics g2 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						new XArc2D(ptStart, ptEnd, ptOnArc).Draw(g2);
						new XArc2D(ptStart, ptEnd, ptOnArc2).Draw(g2);
					}
				}
				catch
				{
				}
			}
		}

		public override void Key(KeyEventArgs keyEventArgs)
		{
			base.Key(keyEventArgs);
			if (keyEventArgs.KeyData == Keys.Space)
			{
				this.SetJigStage();
			}
		}

		private void SetJigStage()
		{
			if (this.JigStage == DuctVJigPolarAxis.EStage.eCenterPt)
			{
				this.JigStage = DuctVJigPolarAxis.EStage.eLeftUpPt;
				this.nTypeStation = 1;
			}
			else if (this.JigStage == DuctVJigPolarAxis.EStage.eLeftUpPt)
			{
				this.JigStage = DuctVJigPolarAxis.EStage.eLeftDownPt;
				this.nTypeStation = 2;
			}
			else if (this.JigStage == DuctVJigPolarAxis.EStage.eLeftDownPt)
			{
				this.JigStage = DuctVJigPolarAxis.EStage.eRightDownOper;
				this.nTypeStation = 3;
			}
			else if (this.JigStage == DuctVJigPolarAxis.EStage.eRightDownOper)
			{
				this.JigStage = DuctVJigPolarAxis.EStage.eRightUpOper;
				this.nTypeStation = 4;
			}
			else if (this.JigStage == DuctVJigPolarAxis.EStage.eRightUpOper)
			{
				this.JigStage = DuctVJigPolarAxis.EStage.eCenterPt;
				this.nTypeStation = 0;
			}
			DuctVJigPolarAxis.SetCursorPos(DuctVJigPolarAxis.ScreenCursorPosition.X, DuctVJigPolarAxis.ScreenCursorPosition.Y);
			this.block = true;
		}

		private XYZ SetPtCur(XYZ ptCur, double dWidth, double dHeight)
		{
			if (dHeight == 0.0)
			{
				return ptCur;
			}
			if (this.JigStage == DuctVJigPolarAxis.EStage.eLeftUpPt)
			{
				ptCur = new XYZ(ptCur.X - dWidth / 2.0, ptCur.Y + dHeight / 2.0, ptCur.Z);
			}
			else if (this.JigStage == DuctVJigPolarAxis.EStage.eLeftDownPt)
			{
				ptCur = new XYZ(ptCur.X - dWidth / 2.0, ptCur.Y - dHeight / 2.0, ptCur.Z);
			}
			else if (this.JigStage == DuctVJigPolarAxis.EStage.eRightDownOper)
			{
				ptCur = new XYZ(ptCur.X + dWidth / 2.0, ptCur.Y - dHeight / 2.0, ptCur.Z);
			}
			else if (this.JigStage == DuctVJigPolarAxis.EStage.eRightUpOper)
			{
				ptCur = new XYZ(ptCur.X + dWidth / 2.0, ptCur.Y + dHeight / 2.0, ptCur.Z);
			}
			return ptCur;
		}

		public List<Line> resultLines = new List<Line>();

		private double dWidth;

		private double dHeight;

		private FormCreateDuct cdt;

		private bool blStauts;

		private int nType = 1;

		public int nTypeStation;

		private bool block;

		public new enum EStage
		{
			eCenterPt,
			eLeftUpPt,
			eLeftDownPt,
			eRightDownOper,
			eRightUpOper
		}
	}
}
